package xw.qxypt.modules.api.service;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xw.qxypt.common.utils.R;
import xw.qxypt.modules.baseinfo.entity.LhpDataRoadDetectorEntity;
import xw.qxypt.modules.baseinfo.entity.LhpDataVisibilityEntity;
import xw.qxypt.modules.baseinfo.entity.LhpDataWeatherStationEntity;
import xw.qxypt.modules.baseinfo.entity.LhpInstallationPointEntity;
import xw.qxypt.modules.baseinfo.service.*;

import java.text.SimpleDateFormat;
import java.util.*;


@Service("ApiService")
public class ApiService{
    @Autowired
    private LhpDataVisibilityService lhpDataVisibilityService;
    @Autowired
    private LhpDataWeatherStationService lhpDataWeatherStationService;
    @Autowired
    private LhpDataRoadDetectorService lhpDataRoadDetectorService;
    @Autowired
    private LhpInstallationPointService lhpInstallationPointService;

    public JSONObject getDataByPointId(String pointId){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LhpInstallationPointEntity point=lhpInstallationPointService.getById(pointId);
        LhpDataVisibilityEntity visibility=lhpDataVisibilityService.getDataVisibilityByPointId(pointId);
        LhpDataWeatherStationEntity weatherStation=lhpDataWeatherStationService.getDataWeatherStationByPointId1(pointId);
        LhpDataRoadDetectorEntity roadDetector=lhpDataRoadDetectorService.getDataRoadDetectorByPointId(pointId);
        JSONObject object =new JSONObject();

        object.put("pointId",pointId);
        object.put("pointName",point.getPointName());
        if(visibility!=null){
            JSONObject visibilityObject =new JSONObject();
            visibilityObject.put("visibility",visibility.getVisibility());
            visibilityObject.put("dataTime",df.format(visibility.getDataTime()));
            object.put("visibilityData",visibilityObject);
        }else {
            object.put("visibilityData","");
        }
        if(weatherStation!=null){
            JSONObject weatherStationObject =new JSONObject();
            weatherStationObject.put("pressure",weatherStation.getPressure());
            weatherStationObject.put("windSpeed",weatherStation.getWindSpeed());
            weatherStationObject.put("windDirection",weatherStation.getWindDirection());
            weatherStationObject.put("precipitation",weatherStation.getPrecipitation());
            weatherStationObject.put("precipitationIntensity",weatherStation.getPrecipitationIntensity());
           // weatherStationObject.put("ambientTemp",weatherStation.getAmbientTemp());
            weatherStationObject.put("dataTime",df.format(weatherStation.getDataTime()));
            object.put("weatherStationData",weatherStationObject);
        }else {
            object.put("weatherStationData","");
        }
        if(roadDetector!=null){
            JSONObject roadDetectorObject =new JSONObject();
            roadDetectorObject.put("roadSurfaceTemp",roadDetector.getRoadSurfaceTemp());
            roadDetectorObject.put("salineConcentrationNacl",roadDetector.getSalineConcentrationNacl());
            roadDetectorObject.put("salineConcentrationCacl",roadDetector.getSalineConcentrationCacl());
            roadDetectorObject.put("salineConcentrationMgcl",roadDetector.getSalineConcentrationMgcl());
            roadDetectorObject.put("freezingTemp",roadDetector.getFreezingTempNacl());
            roadDetectorObject.put("waterFilmHeight",roadDetector.getWaterFilmHeight());
            roadDetectorObject.put("friction",roadDetector.getFriction());
            roadDetectorObject.put("roadCondition",roadDetector.getRoadCondition());
            roadDetectorObject.put("icePercentage",roadDetector.getIcePercentage());
            roadDetectorObject.put("dataTime",df.format(roadDetector.getDataTime()));
            object.put("roadDetectorData",roadDetectorObject);
        }else {
            object.put("roadDetectorData","");
        }
        return object;
    }

    public Map<String, JSONObject> getDataAll(){
        String waysectId="2c9d254162fac6480162fac8ce300006";
        List<LhpInstallationPointEntity> pointList=lhpInstallationPointService.list(
                new QueryWrapper<LhpInstallationPointEntity>()
                        .eq("road_id",waysectId)
        );
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -5);
        List<LhpDataVisibilityEntity> visibilityList=lhpDataVisibilityService.list(
                new QueryWrapper<LhpDataVisibilityEntity>()
                        .gt("data_time",c.getTime())
                        .eq("waysect_id",waysectId)
        );
       /* List<LhpDataWeatherStationEntity> weatherStationList=lhpDataWeatherStationService.list(
                new QueryWrapper<LhpDataWeatherStationEntity>()
                        .gt("data_time",c.getTime())
                        .eq("waysect_id",waysectId)
        );*/
        List<LhpDataRoadDetectorEntity> roadDetectorList=lhpDataRoadDetectorService.list(
                new QueryWrapper<LhpDataRoadDetectorEntity>()
                        .gt("data_time",c.getTime())
                        .eq("waysect_id",waysectId)
        );

        Map<String,JSONObject> dataMap=new HashMap<>();
        for (LhpInstallationPointEntity point:pointList) {
            if(!dataMap.containsKey(point.getId())){
                JSONObject object=new JSONObject();
                object.put("pointId",point.getId());
                object.put("pointName",point.getPointName());
                object.put("visibilityData","");
               // object.put("weatherStationData","");
                object.put("roadDetectorData","");

                dataMap.put(point.getId(),object);
            }
        }
        for (LhpDataVisibilityEntity visibility:visibilityList) {
            JSONObject visibilityObject =new JSONObject();
            visibilityObject.put("visibility",visibility.getVisibility());
            visibilityObject.put("dataTime",df.format(visibility.getDataTime()));
            dataMap.get(visibility.getPointId()).put("visibilityData",visibilityObject);
        }
       /* for (LhpDataWeatherStationEntity weatherStation:weatherStationList) {
            JSONObject weatherStationObject =new JSONObject();
            weatherStationObject.put("pressure",weatherStation.getPressure());
            weatherStationObject.put("windSpeed",weatherStation.getWindSpeed());
            weatherStationObject.put("windDirection",weatherStation.getWindDirection());
            weatherStationObject.put("precipitation",weatherStation.getPrecipitation());
            weatherStationObject.put("precipitationIntensity",weatherStation.getPrecipitationIntensity());
            weatherStationObject.put("ambientTemp",weatherStation.getAmbientTemp());
            weatherStationObject.put("dataTime",df.format(weatherStation.getDataTime()));
            dataMap.get(weatherStation.getPointId()).put("weatherStationData",weatherStationObject);
        }*/
        for (LhpDataRoadDetectorEntity roadDetector:roadDetectorList) {
            JSONObject roadDetectorObject =new JSONObject();
            roadDetectorObject.put("roadSurfaceTemp",roadDetector.getRoadSurfaceTemp());
            roadDetectorObject.put("salineConcentrationNacl",roadDetector.getSalineConcentrationNacl());
            roadDetectorObject.put("salineConcentrationCacl",roadDetector.getSalineConcentrationCacl());
            roadDetectorObject.put("salineConcentrationMgcl",roadDetector.getSalineConcentrationMgcl());
            roadDetectorObject.put("freezingTemp",roadDetector.getFreezingTempNacl());
            roadDetectorObject.put("waterFilmHeight",roadDetector.getWaterFilmHeight());
            roadDetectorObject.put("friction",roadDetector.getFriction());
            roadDetectorObject.put("roadCondition",roadDetector.getRoadCondition());
            roadDetectorObject.put("icePercentage",roadDetector.getIcePercentage());
            roadDetectorObject.put("dataTime",df.format(roadDetector.getDataTime()));
            dataMap.get(roadDetector.getPointId()).put("roadDetectorData",roadDetectorObject);
        }
        return dataMap;
    }



}
